package com.xj.rpc;

import java.lang.reflect.Proxy;

/**
 * Created by xiongjie on 2020/2/23.
 */
public class RpcClient {
    private RpcClientConfig config;
    private Encoder encoder;
    private Decoder decoder;
    private TransportSelector selector;

    public RpcClient(){
        this(new RpcClientConfig());
    }

    public RpcClient(RpcClientConfig config){
        this.config=config;
        this.encoder=ReflectionUtils.newInstance(this.config.getEncodeClass());
        this.decoder=ReflectionUtils.newInstance(this.config.getDecodeClass());
        this.selector=ReflectionUtils.newInstance(this.config.getSelectorClass());

        this.selector.init(this.config.getServers(),this.config.getConnectCount(),this.config.getTransportClass());
    }

    public <T> T getProxy(Class<T> clazz){
        return (T) Proxy.newProxyInstance(
                getClass().getClassLoader(),
                new Class[]{clazz},
                new RemoteInvoker(clazz,encoder,decoder,selector)
        );
    }

}
